#include <iostream>
#include <cstdio>
#include <string>
#include <unistd.h>
#include <mysql/mysql.h>
// const std::string host = "127.0.0.1";
const std::string host = "localhost";
const std::string user = "xzq";
const std::string passwd = "Xzq20031003zshxf.";
const std::string db = "conn";
const unsigned int port = 3306;
int main()
{
    // printf("mysql client Version: %s\n", mysql_get_client_info());
    MYSQL *my = mysql_init(nullptr);
    if (my == nullptr)
    {
        std::cerr << "init MySQL error" << std::endl;
        return 1;
    }
    if (mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        std::cerr << "connect MySQL error" << std::endl;
        return 2;
    }
    mysql_set_character_set(my, "utf8");
    // std::string sql = "insert into user values (2,'zhangshouhua',50,'13763693102')";
    // std::string sql = "update user set name='xzq1' where id=1";
    // std::string sql = "delete from user where id=1";
    // std::string sql = "delete from user where id=2";
    // std::string sql = "insert into user values (1,'辛紫秋',23,'15245641013')";
    // std::string sql = "insert into user values (2,'张守华',50,'13763693102')";
    // std::string sql = "insert into user values (3,'辛富',51,'15245641014')";
    // std::string sql = "select * from user";
    std::string sql = "select name,age from user where id=1";
    int n = mysql_query(my, sql.c_str());
    if (n == 0)
    {
        std::cout << sql << " success " << std::endl;
    }
    else
    {
        std::cerr << sql << " failed " << std::endl;
        return 3;
    }
    MYSQL_RES *res = mysql_store_result(my);
    if (res == nullptr)
    {
        std::cerr << "mysql_store_result error" << std::endl;
        return 4;
    }
    // my_ulonglong rows = mysql_num_rows(res);
    // my_ulonglong fields = mysql_num_fields(res);
    int rows = mysql_num_rows(res);
    int fields = mysql_num_fields(res);
    std::cout << "行: " << rows << std::endl;
    std::cout << "列: " << fields << std::endl;
    // 属性
    MYSQL_FIELD *fields_array = mysql_fetch_fields(res);
    for (int i = 0; i < fields; i++)
    {
        std::cout << fields_array[i].name << "\t";
    }
    std::cout << std::endl;
    // 内容
    for (int i = 0; i < rows; i++)
    {
        MYSQL_ROW row = mysql_fetch_row(res);
        for (int j = 0; j < fields; j++)
        {
            std::cout << row[j] << "\t"; //*(row+j)
        }
        std::cout << std::endl;
    }
    std::cout << fields_array[0].db << " " << fields_array[0].table << std::endl;
    // std::string sql;
    // while (true)
    // {
    //     std::cout << "MySQL>>> ";
    //     if (!getline(std::cin, sql) || sql == "quit")
    //     {
    //         std::cout << "bye bye" << std::endl;
    //         break;
    //     }
    //     int n = mysql_query(my, sql.c_str());
    //     if (n == 0)
    //     {
    //         std::cout << sql << " success : " << n << std::endl;
    //     }
    //     else
    //     {
    //         std::cout << sql << " failed : " << n << std::endl;
    //     }
    // }
    // sleep(10);
    // std::cout << "connect success" << std::endl;
    mysql_free_result(res);
    mysql_close(my);
    return 0;
}
